<?php
/**
 * Created by PhpStorm.
 * securityr: VJLau
 * Date: 2018/1/30
 * Time: 下午3:12
 */

load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT * FROM "
        . tablename('bontrip_insurance') . " WHERE uniacid = '{$uniacid}' and deleted_at is null"
        . " ORDER BY id DESC LIMIT "
        . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM "
        . tablename('bontrip_insurance') . "WHERE uniacid = '{$uniacid}' and deleted_at is null");
    $pager = pagination($total, $pindex, $psize);
} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $uniacid,
            'name' => $_GPC['name'],
            'thumb' => $_GPC['thumb'],
            'updated_at' => TIMESTAMP
        );
        $security = $_GPC['security'];
        $notice = $_GPC['notice'];
        //重置索引
        if (!empty($security["security"])) {
            $security['security'] = array_values($security["security"]);
        }
        if (!empty($notice["notice"])) {
            $notice['notice'] = array_values($notice["notice"]);
        }
        $data['security_desc'] = serialize($security);
        $data['notice_desc'] = serialize($notice);

        if (!empty($id)) {
            pdo_update('bontrip_insurance', $data, array('id' => $id));
        } else {
            $data['created_at'] = $data['updated_at'];
            pdo_insert('bontrip_insurance', $data);
        }
        message('保存保险类型成功！', $this->createWebUrl('insurance', array('op' => 'display')), 'success');
    }
    $insurance = pdo_fetch("select * from " . tablename('bontrip_insurance') . " where id={$id} and uniacid={$uniacid}");
    if (!empty($insurance['security_desc'])) {
        $insurance['security_desc'] = unserialize($insurance['security_desc']);
    }
    $security_num = empty($insurance["security_desc"]["security"]) ? 0 : count($insurance["security_desc"]["security"]);

    if (!empty($insurance['notice_desc'])) {
        $insurance['notice_desc'] = unserialize($insurance['notice_desc']);
    }
    $notice_num = empty($insurance["notice_desc"]["notice"]) ? 0 : count($insurance["notice_desc"]["notice"]);

} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $insurance = pdo_fetch("SELECT id FROM " . tablename('bontrip_insurance')
        . " WHERE id={$id} AND uniacid={$uniacid} AND deleted_at is null");

    if (empty($insurance)) {
        message('抱歉，保险类型不存在或是已经被删除！', $this->createWebUrl('insurance', array('op' => 'display')), 'error');
    }
    pdo_update('bontrip_insurance', array('deleted_at' => time()), array('id' => $id));
    pdo_delete('ims_bontrip_insurance_price', array('deleted_at' => time()), array('insurance_id' => $id));

    message('保险类型删除成功！', $this->createWebUrl('insurance', array('op' => 'display')), 'success');

} else {
    message('请求方式不存在');

}
include $this->template('insurance', TEMPLATE_INCLUDEPATH, true);